package com.example.culturapp;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Calendar;

import android.app.ActionBar;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class SearchActivity extends ActionBarActivity{
	private TextView mDateDisplay;
	private Button mPickDate;
	private ListView listView;
	private CheckBox checkBoxNombre;
	private CheckBox checkBoxFecha;
	private EditText nombre;
	private Model model;
	private int mYear;    
	private int mMonth;    
	private int mDay;
	static final int DATE_DIALOG_ID = 0;
	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

		public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
			mYear = year;
			mMonth = monthOfYear;
			mDay = dayOfMonth;
			updateDisplay();
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		final ActionBar actionBar = getActionBar();
		actionBar.setIcon(R.drawable.ico);
		actionBar.setDisplayShowTitleEnabled(false);
		actionBar.setDisplayHomeAsUpEnabled(false);
		setContentView(R.layout.search);
		
		// capture our View elements
				mDateDisplay = (TextView) findViewById(R.id.fecha);
				mPickDate = (Button) findViewById(R.id.pickDate);
				// add a click listener to the button
				mPickDate.setOnClickListener(new View.OnClickListener(){
					public void onClick(View v) {
						showDialog(DATE_DIALOG_ID);
					}
				});
				// get the current date

				final Calendar c = Calendar.getInstance();
				if(savedInstanceState!=null){
				int aux=savedInstanceState.getInt("mYear", 0);
				//if(aux==0)
				//	mYear = c.get(Calendar.YEAR);
				//else
					mYear=aux;
				aux=savedInstanceState.getInt("mMonth", 0);
				//if(aux==0)
				//	mMonth = c.get(Calendar.MONTH);
				//else
					mMonth =aux;
				aux=savedInstanceState.getInt("mDay", 0);
				//if(aux==0)
				//	mDay = c.get(Calendar.DAY_OF_MONTH);
				//else
					mDay =aux;
					
				}else{
					mYear = c.get(Calendar.YEAR);
					mMonth = c.get(Calendar.MONTH);
					mDay = c.get(Calendar.DAY_OF_MONTH);
				}
				// display the current date (this method is below)
				updateDisplay();
		
				checkBoxNombre = (CheckBox) findViewById(R.id.checkBoxNombre);
				checkBoxFecha = (CheckBox) findViewById(R.id.checkBoxFecha);
				nombre = (EditText) findViewById(R.id.nombre);
				listView = (ListView) findViewById(R.id.listView);
				Button buscar = (Button) findViewById(R.id.buscar);
				
				model = Model.getInstance();
				
				buscar.setOnClickListener(new View.OnClickListener(){
					public void onClick(View v) {
						if(checkBoxFecha.isChecked() && checkBoxNombre.isChecked()){
							ItemAdapterHoy listAdapter = new ItemAdapterHoy(getBaseContext(), model.search(nombre.getText()+"",mDateDisplay.getText()+""));
							listView.setAdapter(listAdapter);
						}else if(checkBoxNombre.isChecked()){
							ItemAdapterHoy listAdapter = new ItemAdapterHoy(getBaseContext(), model.searchName(nombre.getText()+""));
							listView.setAdapter(listAdapter);
						}else if(checkBoxFecha.isChecked()){
							ItemAdapterHoy listAdapter = new ItemAdapterHoy(getBaseContext(), model.searchDate(mDateDisplay.getText()+""));
							listView.setAdapter(listAdapter);
						}
					}
				});
				//ItemAdapterHoy listAdapter = new ItemAdapterHoy(this, items);
				
	}
	
	private void updateDisplay() {
		mDateDisplay.setText(
				new StringBuilder()
				// Month is 0 based so add 1
				.append(mDay).append("-")
				.append(mMonth + 1).append("-")
				.append(mYear).append(" "));
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt("mYear", mYear);
		outState.putInt("mMonth", mMonth);
		outState.putInt("mDay", mDay);
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {   
		case DATE_DIALOG_ID:
			return new DatePickerDialog(this,
					mDateSetListener,
					mYear, mMonth, mDay);  
		}    
		return null;
	}
	
	@Override
	public void onResume() {
		super.onResume();
		//this.getListView().setAdapter(listAdapter);
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapter, View view,
					int position, long arg) {
				ItemHoy item = (ItemHoy) listView.getAdapter().getItem(position);
				
				//intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				if(view.getContext()!=null){
					Intent intent = new Intent(view.getContext(), MostrarEventoActivity.class);
					intent.putExtra("nombre",item.getNombre());
					intent.putExtra("lugar",item.getLugar());
					intent.putExtra("fecha",item.getFecha());
					intent.putExtra("descripcion",item.getDescripcion());
					intent.putExtra("hora",item.getHora());
					Bitmap bitmap;
			        int img=0;
			       	try{
			       		bitmap = (Bitmap) item.getImagen();
			   		}catch(Exception e){
			   			bitmap= null;
			   			img = (Integer) item.getImagen();
			   		}
			       	if(bitmap==null){
			       		intent.putExtra("imagen",(Integer)item.getImagen());
			       		intent.putExtra("isBitmap", false);
			       	}else{
			       		ByteArrayOutputStream stream = new ByteArrayOutputStream();
			       		bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
			       		byte[] byteArray = stream.toByteArray();
			       		intent.putExtra("bitmap", byteArray);
			       		intent.putExtra("isBitmap", true);
			       	}
					view.getContext().startActivity(intent);
				}else{
					Log.d(null, "***************************NULL");
				}
			};

		});
	};
}
